from django.db import models


# Create your models here.
class BaseModel(models.Model):
    tenant_id = models.CharField(max_length=32, null=True)
    revision = models.IntegerField(null=True)
    created_by = models.CharField(max_length=32, null=True)
    created_time = models.DateTimeField(null=True)
    updated_by = models.CharField(max_length=32, null=True)
    updated_time = models.DateTimeField(null=True)
    remark = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    class Meta:
        abstract = True

    PAY_TYPE = ((1, "现金"), (2, "支付宝"), (3, "微信"), (4, "银行转账"))
    PAY_MODE = ((0, "其他"), (1, "月付"), (2, "季付"), (3, "半年付"), (4, "年付"))


class SupplierModel(BaseModel):
    name = models.CharField(max_length=90, verbose_name="名称")
    contact_no = models.CharField(max_length=32, verbose_name="联系电话", null=True, blank=True)
    contact_name = models.CharField(max_length=90, verbose_name="联系人", null=True, blank=True)


class BuyOrderModel(BaseModel):
    supplier = models.ForeignKey(SupplierModel, null=True, on_delete=models.SET_NULL, related_name='buy_orders')
    buy_date_time = models.DateTimeField(verbose_name='日期')
    digest = models.CharField(max_length=128, verbose_name='摘要')
    amount = models.DecimalField(max_digits=24, decimal_places=6, verbose_name='数量')
    price = models.DecimalField(max_digits=24, decimal_places=6, verbose_name='金额')
    unit = models.CharField(max_length=32, verbose_name='单位', null=True, blank=True)
    is_payed = models.IntegerField(verbose_name='已付款', default=0)
    is_recv_invoice = models.IntegerField(verbose_name='已收发票', default=0)
    is_take_delivery = models.IntegerField(verbose_name='已收货', default=0)

    class Meta:
        indexes = [
            models.Index(fields=["is_payed"], name="IDX_IS_PLAYED_BUY_ORDER"),
            models.Index(fields=["is_recv_invoice"], name="IDX_IS_RECV_INVOICE_BUY_ORDER"),
            models.Index(fields=["is_take_delivery"], name="IDX_IS_TAKE_DELIVERY_ORDER"),
        ]


class RecvInvoiceModel(BaseModel):
    recv_invoice_date_time = models.DateTimeField(verbose_name='接收发票时间')
    recv_invoice_no = models.CharField(max_length=255, verbose_name="发票号码", null=True, blank=True)
    recv_invoice_file_url = models.CharField(max_length=255, verbose_name="发票文件路径", null=True, blank=True)
    buy_order = models.ForeignKey(BuyOrderModel, null=True, on_delete=models.SET_NULL, related_name='recv_invoices')


class Payment4BuyOrderModel(BaseModel):
    pay_date_time = models.DateTimeField(verbose_name="付款日期")
    pay_type = models.IntegerField(choices=BaseModel.PAY_TYPE, verbose_name="付款方式")
    amount = models.DecimalField(max_digits=24, decimal_places=6, verbose_name="金额")
    pay_flow_seq = models.CharField(max_length=255, verbose_name="流水号", null=True, blank=True)
    buy_order = models.ForeignKey(BuyOrderModel, null=True, on_delete=models.SET_NULL, related_name='payments')


class TakeDeliveryModel(BaseModel):
    take_delivery_date_time = models.DateTimeField(verbose_name='收货日期')
    take_delivery_no = models.CharField(max_length=255, verbose_name="快递编号", null=True, blank=True)
    buy_order = models.ForeignKey(BuyOrderModel, null=True, on_delete=models.SET_NULL, related_name='deliveries')
